const path = require('path');
const fs = require('fs');
const os = require('os');

const filepath = path.resolve('./data/user.txt');

/* fs.writeFile( filepath , 'haha' , err=>{
    if( !err ){
        console.log('写入成功');
    }else{
        console.log('失败');
    }
} ) */

/* fs.writeFile( filepath , 'haha'+os.EOL , {flag:'a'} , err=>{
    if( !err ){
        console.log('写入成功');
    }else{
        console.log('失败');
    }
} ) */

/* fs.appendFile( filepath , 'heihei11'+os.EOL , err=>{
    if( !err ){
        console.log('写入成功');
    }else{
        console.log('失败');
    }
} ) */

/* fs.readFile( filepath , ( err , data )=>{
    if( !err ){
        console.log(data.toString());
    }else{
        // 抛异常
        throw new Error(err);
    }
} ) */

// 直接转换为utf-8编码的字符串
/* fs.readFile( filepath , 'utf-8' , (err,data)=>{
    if( !err ){
        console.log(data);
    }else{
        throw new Error (err);
    }
} ) */

/* function readFile(filepath){
    return new Promise( ( f,r )=>{
        fs.readFile( filepath , 'utf-8' , (err,data)=>{
            if( !err ){
                f(data);
            }else{
                r(err);
            }
        } )
    } )
} */

// console.log( fs.existsSync(filepath) );

const stat = fs.statSync(filepath);
// console.log(stat);

// console.log( stat.isDirectory() );
// console.log( stat.isFile() );

// console.log( stat.mtime );

// console.log( stat.size );

const dirpath = path.resolve('./data');
fs.readdir( dirpath , 'utf-8' , (err,data)=>{
    console.log(data);
} )

fs.unlinkSync(filepath);